package com.yeskery.nut.extend.jdbc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Sql链操作接口
 * @author sprout
 * 2025-10-10 15:27
 */
public class DefaultSqlChainOperate implements SqlChainOperate {

    /** SQL链 */
    private final List<SqlChain> sqlChains = new ArrayList<>();

    /** SQL参数 */
    private final List<Object> sqlParams = new ArrayList<>();

    /** JdbcSqlBuilder */
    private final JdbcSqlBuilder jdbcSqlBuilder;

    /**
     * 构造函数
     * @param jdbcSqlBuilder JdbcSqlBuilder
     */
    DefaultSqlChainOperate(JdbcSqlBuilder jdbcSqlBuilder) {
        this.jdbcSqlBuilder = jdbcSqlBuilder;
    }

    @Override
    public SqlChainOperate and() {
        return append(SqlChainOperate.AND_SQL_CHAIN, sqlChains);
    }

    @Override
    public SqlChainOperate or() {
        return append(SqlChainOperate.OR_SQL_CHAIN, sqlChains);
    }

    @Override
    public SqlChainOperate leftBracket() {
        return append("(", sqlChains);
    }

    @Override
    public SqlChainOperate rightBracket() {
        return append(")", sqlChains);
    }

    @Override
    public SqlChainOperate eq(String column, Object value) {
        sqlParams.add(value);
        return append(column + " = ?", sqlChains);
    }

    @Override
    public SqlChainOperate neq(String column, Object value) {
        sqlParams.add(value);
        return append(column + " <> ?", sqlChains);
    }

    @Override
    public SqlChainOperate gt(String column, Object value) {
        sqlParams.add(value);
        return append(column + " > ?", sqlChains);
    }

    @Override
    public SqlChainOperate gte(String column, Object value) {
        sqlParams.add(value);
        return append(column + " >= ?", sqlChains);
    }

    @Override
    public SqlChainOperate lt(String column, Object value) {
        sqlParams.add(value);
        return append(column + " < ?", sqlChains);
    }

    @Override
    public SqlChainOperate lte(String column, Object value) {
        sqlParams.add(value);
        return append(column + " <= ?", sqlChains);
    }

    @Override
    public SqlChainOperate like(String column, Object value) {
        sqlParams.add(value);
        return append(column + " LIKE ?", sqlChains);
    }

    @Override
    public SqlChainOperate notLike(String column, Object value) {
        sqlParams.add(value);
        return append(column + " NOT LIKE ?", sqlChains);
    }

    @Override
    public SqlChainOperate in(String column, Object... values) {
        sqlParams.addAll(Arrays.asList(values));
        return append(column + " IN (" + Arrays.stream(values).map(r -> "?").collect(Collectors.joining(", ")) + ")", sqlChains);
    }

    @Override
    public SqlChainOperate notIn(String column, Object... values) {
        sqlParams.addAll(Arrays.asList(values));
        return append(column + " NOT IN (" + Arrays.stream(values).map(r -> "?").collect(Collectors.joining(", ")) + ")", sqlChains);
    }

    @Override
    public SqlChainOperate between(String column, Object value1, Object value2) {
        sqlParams.addAll(Arrays.asList(value1, value2));
        return append(column + " BETWEEN ? AND ?", sqlChains);
    }

    @Override
    public SqlChainOperate notBetween(String column, Object value1, Object value2) {
        sqlParams.addAll(Arrays.asList(value1, value2));
        return append(column + " NOT BETWEEN ? AND ?", sqlChains);
    }

    @Override
    public SqlChainOperate isNull(String column) {
        return append(column + " IS NULL", sqlChains);
    }

    @Override
    public SqlChainOperate isNotNull(String column) {
        return append(column + " IS NOT NULL", sqlChains);
    }

    /**
     * 获取SQL链
     * @return SQL链
     */
    public List<SqlChain> getSqlChains() {
        return sqlChains;
    }

    /**
     * 获取SQL参数
     * @return SQL参数
     */
    public List<Object> getSqlParams() {
        return sqlParams;
    }

    @Override
    public JdbcSqlBuilder completed() {
        return jdbcSqlBuilder;
    }
}
